﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Core.Web;
using Texfina.Common.Mg;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using RHBOMnt = Texfina.BOMnt.Rh;
using RHBOQry = Texfina.BOQry.Rh;

namespace RH
{
   public partial class wfPorrogaBnd : EditPage
   {

      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();
      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
         this.LockControls();

         //if (this.DataSource != null)
         //{
         //   grdList.DataSource = this.DataSource;
         //   grdList.DataBind();
         //}

      }

      public override void LoadInitialSettings()
      {
          this.FormEditAction = 0;
          //this.FormEditAction = (FormEditAction)Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
          this.SetInitialTitle("Porroga");
      }

      public override void DataToControls()
      {
     
         DataTable dt = (DataTable)Session["Porroga"];
         this.GridSetDataBinding(grdList, dt);
         Session.Remove("Porroga");

      }

      public override void RegisterGeneralScripts()
      {

         StringBuilder sb = new StringBuilder();

         //sb.AppendLine("function validate" + this.bttAgregar.ID + "(){");
         //sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeFeriado.ClientID + "','Seleccione una fecha.');");
         //sb.AppendLine("if(varRpta==true)");
         //sb.AppendLine("return !varRpta;");
         //sb.AppendLine("varRpta = gridFindCellValueInRow('" + grdList.ClientID + "',new Array('" + _GRDLIST_FECHA + "'), new Array(document.getElementById('" + this.txtfeFeriado.ClientID + "').value),'La fecha ya Existe');");
         //sb.AppendLine("if(varRpta==true)");
         //sb.AppendLine("return !varRpta;");


         //sb.AppendLine("return true;");
         //sb.AppendLine("}");

         sb.AppendLine("function validateEditForm(key){");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeFin.ClientID + "','Seleccione fecha Fin de Prorroga.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
         sb.AppendLine("}");


         //bttListAD.OnClientClick = "return validate" + bttListAD.ID + "();";

         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);

         //this.bttAgregar.OnClientClick = "return validate" + bttAgregar.ID + "();";

      }

      public override void LockControls()
      {

         //switch (this.FormEditAction)
         //{
         //   case FormEditAction.Edit:
         //      txtidLogon.Attributes.Add("readonly", "true");
         //      bttListAD.Attributes.Add("disabled", "true");
         //      break;
         //}

         //if (User.Identity.AuthenticationType == ConstAuthenticationType.LDAP)
         //{
         //   txtidLogon.Attributes.Add("readonly", "true");
         //   txtdsNombres.Attributes.Add("readonly", "true");
         //}

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         
         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_SAVE:
            case ConstMenuToolbarValue.BUTTON_SAVENEW:
               if (this.FormEditAction != FormEditAction.Delete)
                 this.SaveData(e.Item.Value);
               else
                  this.DeleteData();
               break;

            case ConstMenuToolbarValue.BUTTON_EXIT:
               this.Response.Redirect("wfPerContratoBnd.aspx");
               break;
         }

      }

      public override void SaveData(String key)
      {
         //if (this.ValidateData() == false)
         //   return;

         IEntityBase objE = (IEntityBase)this.CreateEntitySet();

         RHBOMnt.IPersonal objBO = (RHBOMnt.IPersonal)WCFHelper.GetObject<RHBOMnt.IPersonal>(typeof(RHBOMnt.Personal));

         string[] strRet = objBO.UpdateDataPerContrato(objE);
         string strMsg = "";

         switch (this.FormEditAction)
         {

            case FormEditAction.AddNew:
            case FormEditAction.Copy:
               strMsg = "Se han actualizado los cambios ";
               break;
            default:
    
               break;
         }

         this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "wfPerContratoBnd.aspx");
         ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "msgPB", "MessageOnPostback();", true);
      }

      public override Object CreateEntitySet()
      {

         DataTable dt = (DataTable)this.DataSource;
    
         IList<EPerContrato> colE = new List<EPerContrato>();
         foreach (DataRow dr in dt.Rows)
         {    
               EPerContrato ObjE = new EPerContrato();

               ObjE.EntityState = EntityState.Added;
               ObjE.IdEmpresa = dr["idEmpresa"].ToString();
               ObjE.IdPersonal = dr["idPersonal"].ToString();
               ObjE.NuSecuen = Convert.ToInt32(dr["nuSecuen"].ToString())+1;
               ObjE.FeSuscripcion=Convert.ToDateTime(dr["feFin"].ToString()).AddDays(1);
               ObjE.FeInicio= Convert.ToDateTime(dr["feFin"].ToString()).AddDays(1);
               ObjE.FeFin = Convert.ToDateTime(txtfeFin.Text);
               ObjE.IdMotivFin = "";
               ObjE.IdPuesto = dr["idPuesto"].ToString();
               ObjE.DsFuncion = dr["dsFuncion"].ToString();
               ObjE.IdRegLaboral = dr["idRegLaboral"].ToString();
               ObjE.StMype = dr["stMype"].ToString();
               ObjE.StDiscapacid = dr["stDiscapacid"].ToString();
               ObjE.IdModalidad = dr["idModalidad"].ToString();
               ObjE.IdSubModal = dr["idSubModal"].ToString();
               ObjE.IdTipo = dr["idTipo"].ToString();
               ObjE.IdGrpOcupac = dr["idGrpOcupac"].ToString();
               ObjE.IdMoneda = dr["idMoneda"].ToString();
               ObjE.IdPeriRemu = dr["idPeriRemu"].ToString();
               ObjE.MtRemunera = Convert.ToDecimal(dr["mtRemunera"].ToString());
               ObjE.IdRemVariable = dr["idRemVariable"].ToString();
               ObjE.StRemuEspecie = dr["stRemuEspecie"].ToString();
               ObjE.StRemuMinima = dr["stRemuMinima"].ToString();
               ObjE.StProrroga = ConstFlagEstado.ACTIVADO;
               ObjE.StContrato = ConstFlagEstado.DESACTIVADO;
               ObjE.StAnulado = ConstFlagEstado.DESACTIVADO;

               //EFeriado objEDet = DataHelper.CopyDataRowToEntity<EPerContrato>(ObjE, typeof(EPerContrato));
               colE.Add(ObjE);
               
         }

         EPersonalSet objESet = new EPersonalSet();

         objESet.ColPerContrato = colE;

         return objESet;
        

      }


      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         //this.EditRowIndex = Convert.ToInt32(e.CommandArgument);

   
         //   case ConstGridRowCommand.GRIDDELETE:
         //      //this.lblTitlePopup.Text = "Detalle Planilla (Dar de Baja)";
         //      //this.GridEditAction = GridEditAction.Delete;
         //      //this.mdlPopup.Show();
         //      //this.LoadDataEditPopup(grdList, this.EditRowIndex);
         //      //this.RegisterEditPopupScripts();
         //       DataTable dt = (DataTable)this.DataSource;
         //       DataRow dr = null;
         //       dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
         //       dr["stAnulado"] = ConstFlagEstado.ACTIVADO;
         //       this.GridSetDataBinding(grdList, dt);
         //       //grdList.DataBind();
             
         //      break;
         //}
      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         //if (e.Row.RowType == DataControlRowType.DataRow)
         //{
         //   if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
         //   { e.Row.ForeColor = System.Drawing.Color.Red; }

         //}
      }


   }
}
